#define	FAULT_PRECHARGE	1
#define	FAULT_CONTACTOR	2
#define FAULT_OVERCURR	4
#define FAULT_OVERVOLT	8
#define FAULT_UNDERVOLT	16
#define	FAULT_THROTTLE	32

#define STATE_PRECHARGE		0
#define STATE_IDLE			1
#define STATE_RUNNING_VHZ	2
#define STATE_RUNNING_FOC	3
#define STATE_ERROR			4

//the timer goes 16kHz so each of these is in 16kHz increments
#define PERIOD_MOTOR_CTL	2 //so 8kHz
#define PERIOD_PERIODIC		4 //4kHz 
#define PERIOD_DEBUG		2000 // 8 times per second
#define PERIOD_LED			16000 //one hertz

typedef struct {
	unsigned MotorCtrl:1; //set true if we've got to attend to the motor
	unsigned PeriodicTasks:1; //set true if we should do periodic stuff
	unsigned Precharged:1; //set true if we're fully precharged
	unsigned Running:1; //set true if we've passed precharge and the main contactor is in
	unsigned DriveEnable:1; //pretty obvious...
	unsigned FOC:1; //true if using FOC otherwise V/Hz
	unsigned SendDebug:1; //temporary bit for debugging
	unsigned	:9; //all the unused bits
} CtrlBitField;

extern CtrlBitField ControllerState;
extern volatile int i_b;		
								
extern volatile int i_alpha, i_beta;
extern volatile long int temp_i_alpha, temp_i_b;
extern volatile int i_d, i_q;
extern volatile unsigned int tempOffset1, tempOffset2;
extern volatile long int cos_theta, sin_theta;
extern volatile long int v_d, v_q;
extern volatile long int v_d_small, v_q_small;
extern volatile int v_alpha, v_beta;
extern volatile long int temp_v_beta;
extern volatile int Vr1, Vr2, Vr3;
extern unsigned int PWM_BIAS;
extern unsigned int MAX_PWM;


extern volatile unsigned int T1, T2;		
extern volatile unsigned int Ta, Tb, Tc;  	

extern volatile int rotorFluxAngle; 
extern volatile unsigned int tickCount;
extern volatile unsigned int rotorPos ;
extern unsigned int numberOfPolePairs;
extern long mechanicalSpeed;

extern volatile unsigned int now, previous;
extern volatile unsigned int startTime, totalTime, endTime;

extern volatile unsigned int counter_16k;
extern volatile unsigned int counter_motor_ctl;
extern volatile unsigned int counter_periodic;
extern volatile unsigned int counter_debug;
extern volatile unsigned int counter_led;
extern volatile unsigned long sys_ticks;

extern volatile int overCurrentHasOccurred;
extern volatile unsigned int throttle_feedback;
extern volatile unsigned int throttle_raw;
extern volatile unsigned int throttle_average;

extern volatile unsigned max_current_ref;
extern volatile unsigned current_ref;

extern volatile unsigned throttle_fault_counts;
extern volatile unsigned int fault_bits;

extern int counter1, i, j, streamOffset;
extern int asciiChar;

extern const int _sin[];

extern char serialStream[];
extern int variable[26];	

extern int use_hardware;
extern int throttle_max_setting;
extern int throttle_min_setting;

extern volatile unsigned int hv_raw_input;
extern unsigned int volt_gain; 
extern unsigned int current_gain;
extern unsigned int current_bias;

void delay_ms(unsigned int time);
